import Axios from "axios";
import config from "../conf/config";
import qs from 'qs'
export function request(_config={},router){
  //创建axios实例
  let service=Axios.create({
    baseURL:config.baseApi
  });
  //请求拦截器
  service.interceptors.request.use((config)=>{
    if(config.method.toLocaleLowerCase()==='post'){
      config.data=qs.stringify(config.data);
    }else if(config.method.toLocaleLowerCase()==='file'){
      let formData=new FormData();
      if(config.data instanceof Object){
        for(let key in config.data){
          formData.append(key,config.data[key])
        }
        config.data=formData;
      }
      config.method="post";
    }
    return config;
  },(error)=>{
    return Promise.reject(error)
  });
  //响应拦截器
  service.interceptors.response.use((response)=>{
    // console.log(response);
    if(response.config.url=='/home/user/safe?token=1ec949a15fb709370f'){
      if(response.data.code!=200){
        localStorage.clear();
        router.replace("/login")
      }
    }
    return response;
  },(error)=>{
    return Promise.reject(error)
  })
  return service.request(_config).then(res=>res.data);
}
